// pages/order/index.js
const app = getApp();

let req = require('../../utils/request.js');
let api = require('../../utils/api.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModalStatus: false,
    total_price: '0.00',
    discount_code: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this,
      id = options.id;
    _this.setData({ id: id })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this = this,
        id = _this.data.id
    
    wx.showLoading({
      title: '加载中',
    })

    req.request(
      api.getProductAttraction,
      { method: 'POST', data: {id: id}},
      function (res) {
        wx.hideLoading();
        res = res.data[0]
        var list = res.pp_info
        for(var i in list) {
          list[i]['count'] = 0
        }
        
        _this.setData({ list: list, attr_info: res })
      }
    )
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  setCode(e) {
    var _this = this,
      value = e.detail.value;

    _this.setData({discount_code: value})
  },
  Submit(e){
    wx.showLoading({
      title: '订单提交中',
    })
    var _this = this,
      total_price = _this.data.total_price,
      list = _this.data.list,
      discount_code = _this.data.discount_code,
      user_token = app.globalData.userInfo.user_token,
      attr_id = _this.data.id,
      order_count = 0,
      date = _this.data.date,
      time = _this.data.time,
      flag = 0, msg = '';

      for(var i in list) {
        order_count += list[i]['count']
      }

    order_count === 0 ? msg = '请至少选择一张票' : date == undefined ? msg = '请选择预约日期' : flag = 1;

    if (!flag) {
      wx.hideLoading();
      wx.showToast({
        title: msg,
        icon: 'none'
      })
      return false;
    }

    var data = {};
    data.total_price = total_price
    data.counts = JSON.stringify(list)
    data.user_token = user_token
    data.order_count = order_count
    data.discount_code = discount_code
    data.attr_id = attr_id
    data.date = date
    data.time = time

    req.request(
      api.createOrder,
      {method: 'POST', data: data},
      function(res) {
        wx.hideLoading();
        res = res.data

        wx.showToast({
          title: res.msg,
          icon: 'none'
        })

        if(res.status == 1) {
          //清除乘客缓存
          wx.removeStorageSync('persons')

          setTimeout(function() {
            wx.redirectTo({
              url: 'check?id='+res.order_id
            })
          }, 2000)
        }
      }
    )
  },
  reduce(e) {
    var id = e.currentTarget.id,
      _this = this,
      list = _this.data.list,
      total_price = parseFloat(_this.data.total_price)

    for(var i in list) {
      if(i == id) {
        if(list[i]['count'] > 0) {
          list[i]['count'] -= 1
          total_price -= parseFloat(list[i]['pri_price']) 
        }
      }
    }
    _this.setData({ total_price: total_price.toFixed(2), list: list})
  },
  addition(e) {
    var id = e.currentTarget.id,
      _this = this,
      list = _this.data.list,
      total_price = parseFloat(_this.data.total_price)

    for (var i in list) {
      if (i == id) {
        list[i]['count'] += 1
        total_price += parseFloat(list[i]['pri_price'])
      }
    }
    _this.setData({ total_price: total_price.toFixed(2), list: list })
  },
  select_time(e) {
    var val = e.detail.value;
    this.setData({time: val})
  },
  select_date(e) {
    var val = e.detail.value;
    this.setData({date: val});
  }
})